// var mongoose = require('mongoose');
var stock = require('tushare').stock;
// var async = require('async');
// var util = require('../util');
// var list = require('../model').list;

var log = require('../util/log').getLog('list');

var page = 1;

var count = {
    add: 0,
    del: 0,
    upd: 0
};

// 使用 getTodayAll接口获取所有的股票代码和名称
function record(page) {

    let pageOpt = {
        pageSize: 100,
        pageNo: page
    };

    stock.getTodayAll(pageOpt).then(({ data }) => {

        if (!data) {
            console.log('end................');
            // return mongoose.disconnect();
            return;
        }

        for (let i = 0, len = data.length; i < len; i++) {
            log.info(data[i].symbol + '|' + data[i].code + '|' + data[i].name);
        }

        // log.info(data);

        page++;
        record(page);

        // async.eachLimit(data, 3, (stock, cb) => {

        //     let condition = {
        //         symbol: stock.symbol
        //     };
        //     let doc = {
        //         symbol: stock.symbol,
        //         code: stock.code,
        //         name: stock.name
        //     };

        //     util.mongo.insertDistinct(list, condition, doc, count, cb);

        // }, err => {
        //     if (err) return console.log(err);
        //     console.log('add logs!');
        //     console.log(count);
        //     page++;
        //     record(page);
        // });
    });
}

record(1);

exports.record = record;